package com.topscomm.mpm.controller;

import com.topscomm.mpm.controller.auto.MpmProjectControllerAuto;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liwanpeng
 * @version 1.0
 * @description:   手机端接口
 * @date 2021/1/21 16:10
 */
@RestController
@RequestMapping("mpm/mobile")
public class MmpMobilController extends MpmProjectControllerAuto {
    @Autowired
    private MpmProjectService mpmProjectService;
    /**
     * 饼状图接口
     *
     * @return
     */
    @GetMapping("/pieDataInfo")
    public ResponseResult<Object> overallProjectInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
        Map<String, Object> result = new HashMap<>();
        // 获取有权限的项目
        List<MpmProjectEntity> projectList = new ArrayList<>();
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" MpmProject.projectstate = 1 and ");
        sql.appendSingle(mpmProjectService.spellSqlForDataBoard(paraMap, "MpmProject", "projectcode"));
        List<MpmProjectEntity> mpmProjectEntities = this.service.queryByWhere(sql.toString());
        // 获取项目状态
        List<Map<String, Object>> projectStateList = datacodeCache.getCodeListMap("MpmProjectAbnormalState", "", "sortcode");
        // 按状态统计
        List<Map<String, Object>> echartsDataForProjectStateForMobile = new ArrayList<>();
        for (Map<String, Object> stat : projectStateList) {
            long categoryCode = Long.parseLong(stat.get("code").toString());
            int categoryNum = 0;
            Map<String, Object> resultTemp = new HashMap<>();
            for (MpmProjectEntity projectEntity : mpmProjectEntities) {
                if (projectEntity.getAbnormalstate() == categoryCode) {
                    categoryNum++;
                }
            }
            resultTemp.put("name", stat.get("name"));
            resultTemp.put("code", stat.get("code"));
            resultTemp.put("value", categoryNum);
            echartsDataForProjectStateForMobile.add(resultTemp);
        }
        // 获取项目全部数量
        Map<String, Object> resultForAllProject = new HashMap<>();
        int totalProjectValue = mpmProjectEntities.size();
        resultForAllProject.put("name", "全部");
        resultForAllProject.put("code", "TotalProject");
        resultForAllProject.put("value", totalProjectValue);
        echartsDataForProjectStateForMobile.add(resultForAllProject);
        result.put("projectStatDataForMobile", echartsDataForProjectStateForMobile);
        return ResponseResult.ok(result);
    }
    /**
     * @description: 项目列表
     * @param: paraMap
     * @return: com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author Administrator
     * @date: 2021/1/21 16:25
     */
    @GetMapping("/projectListForMobile")
    public ResponseResult<Object> projectListForMobile(@RequestParam(required = false) Map<String, Object> paraMap) {
        Map<String, Object> result = new HashMap<>();
        paraMap = BaseUtil.decodeSecureMap(paraMap);
        Query query = new Query(paraMap);
        StringBufferProxy sql = new StringBufferProxy();
        //经营管理部要求项目监管可以看到立项审批中的和立项审批通过的项目
        if(paraMap.containsKey("projectstateflag")){
            sql.appendSingle(" MpmProject.projectstate = 1 and ");
        }else{
            sql.appendSingle(" MpmProject.state in (1,9) and ");
        }
        sql.appendSingle(mpmProjectService.spellSqlForDataBoard(paraMap, "MpmProject", "projectcode"));
        int totalCount = this.service.getCount(sql.toString());
        List<Map<String, Object>> tempList = null;
        if (query.getPageSize() > 0) {
            tempList = this.service.queryMapForPage(sql.toString(), query.getCurrentPage(), query.getPageSize(),
                    query.getSidx(), query.getSord(),"queryMapForPageForMobile");
        } else
            tempList = this.service.queryMapByWhere(sql.toString(),"queryMapByWhereForMobile");
        this.service.setDispFields(tempList);
        Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
        return ResponseResult.ok(page);
    }
}
